<?php

class TariffPlan extends BaseActiveRecord
{
    const PLAN_START = 'start';
    const PLAN_BUSINESS = 'business';
    const PLAN_BANK = 'bank';
    const PLAN_BANK_PLUS = 'bank_plus';
    const PLAN_PROFI = 'profi';
    const PLAN_SELLER = 'seller';
    const PLAN_METALS = 'metals';

    public static function getTariffPlans()
    {
            return array(
                self::PLAN_START => 'Старт',
                self::PLAN_BUSINESS => 'Бизнес',
                self::PLAN_BANK => 'Банк',
                self::PLAN_BANK_PLUS => 'Банк Плюс',
                self::PLAN_PROFI => 'Профи',
                self::PLAN_METALS => 'Металлы',
            );
    }

    public static function getLotsCount()
    {
            return array(
                self::PLAN_START => 2,
                self::PLAN_BUSINESS => 8,
                self::PLAN_BANK => 30,
                self::PLAN_BANK_PLUS => 60,
                self::PLAN_PROFI => 0,
                self::PLAN_METALS => 0,
            );
    }

    public static function getDaysCount()
    {
            return array(
                self::PLAN_START => 7,
                self::PLAN_BUSINESS => 7,
                self::PLAN_BANK => 7,
                self::PLAN_BANK_PLUS => 7,
                self::PLAN_PROFI => 7,
                self::PLAN_METALS => 7,
            );
    }

    public function tableName()
    {
        return '{{tariff_plans}}';
    }

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public static function hasTariffPlan($iUserId, $sPlanName)
    {
        $oTariffPlan = TariffPlan::model()->find('(user_id = '.$iUserId.') AND (tariff_plan = "'.$sPlanName.'") AND (status = "approved") AND (UNIX_TIMESTAMP() < UNIX_TIMESTAMP(date_expired))');
        return (bool)$oTariffPlan;
    }

    public static function hasActiveTariffPlans($iUserId)
    {
        $oTariffPlan = TariffPlan::model()->find('(user_id = '.$iUserId.') AND (tariff_plan <> "'.self::PLAN_SELLER.'") AND (status = "approved") AND (UNIX_TIMESTAMP() < UNIX_TIMESTAMP(date_expired))');
        return (bool)$oTariffPlan;
    }

    public function getAvailableTariffPlans($iUserId)
    {
        $aAvailableTariffPlans = Yii::app()->db->createCommand()
                                        ->select('*')
                                        ->from($this->tableName())
                                        ->where('status = "approved" AND user_id=:user_id AND lots_left > 0 AND UNIX_TIMESTAMP(date_expired) > UNIX_TIMESTAMP()', array(':user_id' => $iUserId))
                                        ->queryAll();
        //print_r($aAvailableTariffPlans);

        return $aAvailableTariffPlans;
    }

    public function setTariffPlanAttributes()
    {
        $aTariffPlans       = self::getTariffPlans();
        $aLotsCount         = self::getLotsCount();
        $aDaysCount         = self::getDaysCount();
        $this->name         = $aTariffPlans[$this->tariff_plan];
        $this->lots_count   = $aLotsCount[$this->tariff_plan];
        $this->lots_left    = $aLotsCount[$this->tariff_plan];
        $this->days_count   = $aDaysCount[$this->tariff_plan];
        $this->date_created = date('Y-m-d H:i:s');
        $this->status       = 'pending';
    }
    
    public function rules() 
    {
        return array
        (
            array('tariff_plan, name, lots_count, lots_left, days_count, status', 'required')
        );
    }
    
    public function attributeLabels()
    {
        return array
        (
        	'tariff_plan_id' => 'ID',
        	'user_id' => 'ID пользователя',
        	'user.user_login' => 'Логин',
                'name' => 'Тарифный план',
                'lots_count'  => 'Количество лотов',
                'lots_left'  => 'Лотов осталось',
                'days_count' => 'Количество дней',
                'date_created' => 'Дата создания',
        	'date_start' => 'Дата утверждения',
	        'date_expired' => 'Дата окончания',
                'status' => 'Статус',
        );
    }
    
    public function relations()
    {
    	return array(
    		'user' => array(self::BELONGS_TO, 'User', 'user_id')
    	);
    }
}